using System;
using System.Linq;
using System.Collections.Generic;

namespace FakeGameEngine
{
    public class GameEngine
    {
        private List<GameObject> _gameObjects;

        private GameEngine() { }
        private static GameEngine _instance = new GameEngine();
        public static GameEngine Instance => _instance ?? (_instance = new GameEngine());

        public void Init(params GameObject[] objs)
        {
            _gameObjects = objs.ToList();
        }

        public void Update()
        {
            foreach (var obj in _gameObjects)
            {
                obj.Update();
            }
        }

        public void YieldNull()
        {
            foreach (var obj in _gameObjects)
            {
                obj.YieldNull();
            }
        }

        public void Run(int loop = -1)
        {
            Time.Reset();
            _gameObjects.ForEach(obj => obj.CheckAwake());

            while (loop != 0)
            {
                Time.Update();
                Console.WriteLine($"[Frame {Time.FrameCount}]---------------");

                Update();
                YieldNull();

                loop--;
            }
        }
    }
}